<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="placeNum"
        placeholder=" 请输入地区"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>
    <el-table

      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      tablder
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <!--      @sort-change="sortChange"-->
      <!--      <el-table-column-->
      <!--        label="序号"-->
      <!--        prop="udOdId"-->
      <!--        sortable="custom"-->
      <!--        align="center"-->
      <!--        width="80"-->
      <!--        :class-name="getSortClass('id')"-->
      <!--      />-->
      <el-table-column prop="index" label="序号" align="center" width="80" fixed>
        <template slot-scope="scope">
          <span>{{ scope.$index +1 }}</span>
        </template>
      </el-table-column>

      <el-table-column label="地区" prop="computerPlace" align="center" />

      <el-table-column label="户型" prop="computerHouse" align="center" />

      <el-table-column label="面积" prop="computerArea" align="center" />

      <el-table-column label="户主姓名" prop="computerName" align="center" />

      <el-table-column label="手机号" prop="computerPhone" align="center" />

      <el-table-column label="创建时间" prop="createTime" align="center" />

      <!--      操作-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="row">

          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDelete(row.row.computerId)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--    分页-->
    <div class="block">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

  </div>
</template>

<script>

import waves from '@/directive/waves' // waves directive

import Pagination from '@/components/Pagination' // secondary package based on el-pagination

import { getcomputer, deletecomputer } from '@/api/computer'

const calendarTypeOptions = [
  { key: 'CN', display_name: 'China' },
  { key: 'US', display_name: 'USA' },
  { key: 'JP', display_name: 'Japan' },
  { key: 'EU', display_name: 'Eurozone' }
]

// arr to obj, such as { CN : "China", US : "USA" }
// eslint-disable-next-line no-unused-vars
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})

export default {
  name: 'ComplexTable',
  // eslint-disable-next-line vue/no-unused-components
  components: { Pagination },
  directives: { waves },
  // filters: {
  //   statusFilter(status) {
  //     const statusMap = {
  //       published: 'success',
  //       draft: 'info',
  //       deleted: 'danger'
  //     }
  //     return statusMap[status]
  //   },
  //   typeFilter(type) {
  //     return calendarTypeKeyValue[type]
  //   }
  // },
  data() {
    return {
      // 表格列表
      list: [],
      // 搜索框
      placeNum: '',
      // 保险弹出
      dialogVisible: false,
      // 设计服务
      dialogVisible2: false,
      // 保险内容
      safe: {},
      designService: {},
      // 分页总数
      total: 0,
      // 当前页
      currentPage: 1,
      // 每页多少条
      pagesizes: 5,

      // 搜索下拉列表
      // importanceOptions: [1, 2, 3],
      tableKey: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },
      calendarTypeOptions,
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }],
      statusOptions: ['published', 'draft', 'deleted'],
      showReviewer: false,
      temp: {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        type: '',
        status: 'published'
      },
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      // 获取订单列表
      getcomputer(this.placeNum, this.currentPage, this.pagesizes).then(res => {
        this.list = res.data.records
        console.log(this.list)

        this.total = res.data.total
        this.listLoading = false
      })
    },
    // 删除
    handleDelete(id) {
      console.log(id)
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletecomputer(id).then(res => {
          console.log(res)
          this.$notify({
            title: res.data,
            message: res.data,
            type: 'success',
            duration: res.code
          })
        })
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 搜索框
    handleFilter() {
      this.getList()
    },
    // 分页
    handleSizeChange(val) {
      this.pagesizes = val
      console.log(this.pagesizes)
      this.getList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      console.log(this.currentPage)
      this.getList()
    },

    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    }
  }
}
</script>
